package cn.baker.app.controller;

import cn.baker.app.common.Constants;
import cn.baker.rabbitmq.sender.TransMessageSender;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import static cn.baker.app.common.Constants.*;
@Slf4j
@RestController
public class SendController {

    /**
     * 需要使用TransMessageSender发送消息，不能使用RabbitTemplate
     */
    @Autowired
    private TransMessageSender transMessageSender;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * http://localhost:8097/sendMessage?payload=1
     */
    @RequestMapping("/sendMessage")
    public boolean test(@RequestParam Integer payload) {
        String exchange = Constants.TRANSACTION_EXCHANGE_NAME;
        String routingKey = Constants.TRANSACTION_KEY;
        // Object payload = "hello";
        transMessageSender.convertAndSend(exchange, routingKey, payload);
        log.info(">>> 发送消息 payload={}", JSON.toJSONString(payload));
        return true;
    }



    /**
     * http://localhost:8097/message/fanoutTest
     */
    @RequestMapping("/message/fanoutTest")
    public String sendFanoutMessage() {
        // RabbitMqDTO.DirectPayLoadDTO message = RabbitMqDTO.DirectPayLoadDTO
        //         .builder()
        //         .type("fanout")
        //         .build();
        String message = "gb";


        String exchange = FANOUT_EXCHANGE;
        // 注意广播模式不需要路由键(改交换机上绑定的所有到队列都会收到消息)
        String routingKey = null;
        rabbitTemplate.convertAndSend(exchange,routingKey,  message);
        return "fanout";
    }

}
